/**
 * Created by jude on 2016/12/20.
 */

/*信息提示*/
function consoleMes($obj,t,tp,mes) {
    var $div=$("<div>").addClass("alert alert-mes").attr({"role":"alert"});
    var $span=$("<span>").addClass("glyphicon");
    if(tp=="success"){
        $div.addClass("alert-success");
        $span.addClass("glyphicon-check");
    }
    if(tp=="info"){
        $div.addClass("alert-primary");
        $span.addClass("glyphicon-info-sign");
    }
    if(tp=="warning"){
        $div.addClass("alert-warning");
        $span.addClass("glyphicon-warning-sign");
    }
    if(tp=="danger"){
        $div.addClass("alert-danger");
        $span.addClass("glyphicon-exclamation-sign");
    }
    $div.append($span,mes);
    $($obj).append($div);
    setTimeout(function () {
        $($obj).find(".alert-mes").remove();
    }, t);
}

/*用户登出*/
function loginOutSystem(){
	/*consoleMes($("#webContent"),2000,"success","系统正在登出！");//success、info、warning、danger
	setTimeout(function () {
    	window.location.href=gobalWeb+"/login.html";
    }, 2500);*/
	$.post(
        gobalServer+"user/logout",
        {},
        function(backData){
            if(backData.statusCode==200){
                /*获取键值：localStorage.getItem(“key”)
                 设置键值：localStorage.setItem(“key”,”value”)
                 清除键值：localStorage.removeItem(“key”)
                 清除所有键值：localStorage.clear()*/
                localStorage.clear();
                consoleMes($("#webContent"),2000,"success",backData.msg);//success、info、warning、danger
                setTimeout(function () {
                    window.location.href=gobalWeb+"login.html";
                }, 2500);
            }else{
                consoleMes($("#webContent"),2000,"warning",backData.msg);//success、info、warning、danger

            }
        }
    );
};


/*左侧导航加载*/
function getNavShow(navData){
	if(navData.length>0){
		eachFirNav(navData);
		//$(".children.collapse").eq(0).addClass("in");
        navCutOver();
	}else{
		consoleMes($("#webContent"),2000,"warning","暂无相关数据！");//success、info、warning、danger
    	setTimeout(function () {
	    	window.location.href=gobalWeb+"login.html";
	    }, 2500);
	}
};
function eachFirNav(dataResult){
    $("#adminMenu").empty();
    if(dataResult.length>0){
        $.each(dataResult,function (index,item) {
            addFirItem(index,item);
        });        
    }else{
    	$("#adminMenu").append(" <li>数据加载出问题</li>");
    }
}
function addFirItem(index,itemlist) {
    if(itemlist.secnav!=null && itemlist.secnav.length>0){
        var $firli=$("<li>").addClass("parent");
        var $fira=$("<a>").addClass("collapse-plus").attr({"href":"javascript:;"}).attr({"data-toggle":"collapse","href":"#sub-item-"+index});
        var $firspan1=$("<span>").addClass("glyphicon glyphicon-list");
        var $firspan2=$("<span>").addClass("icon pull-right");//.attr({"data-toggle":"collapse","href":"#sub-item-"+index});
        var $firem=$("<em>").addClass("glyphicon glyphicon-s glyphicon-plus");
        $firspan2.append($firem);
        $fira.append($firspan1,itemlist.firsttitle,$firspan2);
        $firli.append($fira);
        eachSecNav($firli,itemlist.secnav,index);
        $("#adminMenu").append($firli);
    }else{
        var $firli=$("<li>");
        var $fira=$("<a>").attr({"href":gobalWeb+itemlist.firsturl});
        var $firspan=$("<span>").addClass("glyphicon glyphicon-th");
        $fira.append($firspan,itemlist.firsttitle);
        $firli.append($fira);
        $("#adminMenu").append($firli);
    }
}
function eachSecNav($obj , dataitem,idx){
    if(dataitem.length>0){
        var $secul=$("<ul>").addClass("children collapse").attr({"id":"sub-item-"+idx});
        $.each(dataitem,function (i,item) {
            addSecItem($secul,i,item);
        })
        $($obj).append($secul);
    }else{
    	$($obj).append(" <ul><li>数据加载出问题</li></ul>");
    }
}
function addSecItem($o,i,item) {
    var $secli=$("<li>");
    var $seca=$("<a>").attr({"href":gobalWeb+item.securl});
    $seca.text(item.sectitle);
    $secli.append($seca);
    $($o).append($secli);
}
function navCutOver(){
	$(".collapse-plus").on('click',function(obj){
		$(".children.collapse").removeClass("in");
	});
};
function digitUppercase(n) {
    var fraction = ['角', '分'];
    var digit = [
        '零', '壹', '贰', '叁', '肆',
        '伍', '陆', '柒', '捌', '玖'
    ];
    var unit = [
        ['元', '万', '亿'],
        ['', '拾', '佰', '仟']
    ];
    var head = n < 0 ? '欠' : '';
    n = Math.abs(n);
    var s = '';
    for (var i = 0; i < fraction.length; i++) {
        s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
    }
    s = s || '整';
    n = Math.floor(n);
    for (var i = 0; i < unit[0].length && n > 0; i++) {
        var p = '';
        for (var j = 0; j < unit[1].length && n > 0; j++) {
            p = digit[n % 10] + unit[1][j] + p;
            n = Math.floor(n / 10);
        }
        s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
    }
    return head + s.replace(/(零.)*零元/, '元')
            .replace(/(零.)+/g, '零')
            .replace(/^整$/, '零元整');
};